﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections;
using Microsoft.SharePoint.Workflow;

namespace SPArtistic.Layouts.MobileApproval
{
    public partial class Approval : System.Web.UI.MobileControls.MobilePage
    {
        private Guid taskGuid;
        private string taskListName = "任务";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["TaskID"] != null)
            {
                taskGuid = new Guid(Request["TaskID"]);
            }
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("MyTasks.aspx");
        }
        protected void cmdSubmit_Click(object sender, EventArgs e)
        {
            string isApprove = "";
            string isAmend = "";
            System.Collections.Hashtable wfProperties = new Hashtable();
            wfProperties.Add("TaskName", "");
            wfProperties.Add("afterCommonts", "");
            wfProperties.Add("Instruction", "");
            wfProperties.Add("Comment", txtComment.Text);
            switch (int.Parse(slApprovae.Selection.Value))
            {
                case 2:
                    isApprove = "false";
                    isAmend = "true";
                    break;
                case 1:
                    isApprove = "true";
                    isAmend = "false";
                    break;
                case 0:
                    isApprove = "false";
                    isAmend = "false";
                    break;
            }
            wfProperties.Add("IsApprove", isApprove);
            wfProperties.Add("IsAmend", isAmend);

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite mSite = new SPSite(SPContext.Current.Web.Url))
                {
                    SPWeb mWeb = mSite.OpenWeb();
                    mWeb.AllowUnsafeUpdates = true;
                    mSite.AllowUnsafeUpdates = true;

                    SPList mList = mWeb.Lists[taskListName];

                    SPListItem mItem = mList.Items[taskGuid];
                    SPWorkflowTask.AlterTask(mItem, wfProperties, true);
                    mWeb.Close();
                    mWeb.Dispose();
                    mSite.Close();
                    mSite.Dispose();
                }
            });

            Response.Redirect("MyTasks.aspx");

        }
    }
}
